<template>
  <div class="flexContent">
    <div class="searchBox">

      <el-form class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="门店名称">
                  <el-input v-model.trim="name" clearable style="width: 95%" placeholder="门店名称"
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="省市区域">
                  <el-cascader ref="cascaderAddr" v-model="selectedOptions" clearable style="width: 95%"
                    :options="options" placeholder="请选择地区" expand-trigger="hover" @change="handleChange" filterable />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" v-has="'shop_add'" @click="addFn">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1" :row-config="{ isHover: true }"
          :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%" size="medium" align="center">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="number" title="门店编号" :min-width="elTableAdaptationWidth('number', shopList)"/>
          <vxe-column field="name" title="门店名称" :min-width="elTableAdaptationWidth('name', shopList)"/>
          <vxe-column field="userName" title="门店账号" min-width="150"/>
          <vxe-column field="merchantId" title="商户号" min-width="150"/>
          <vxe-column field="ratio" title="分账比例" min-width="100"/>
          <vxe-column field="region" title="省市区域" :min-width="elTableAdaptationWidth('region', shopList)"/>
          <vxe-column field="storeState" title="状态" width="100">
            <template v-slot="{ row }">
              <el-tag v-if="row.storeState === '0'" disabled type="primary" plain size="small">经营中</el-tag>
              <el-tag v-if="row.storeState === '1'" disabled type="info" plain size="small">已暂停</el-tag>
            </template>
          </vxe-column>
          <vxe-column v-if="$showFn(['shop_detail', 'shop_edit', 'shop_copy'])" fixed="right" width="200" title="操作">
            <template v-slot="{ row }">
              <el-button type="text" v-has="'shop_detail'" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="shopDetail(row)">详情</el-button>
              <el-button style="color: rgb(0, 132, 255)" type="text" v-has="'shop_edit'"
                @click.stop="editPassword(row)">修改密码</el-button>
              <el-button style="color: rgb(0, 132, 255)" type="text" v-has="'shop_edit'"
                @click.stop="editShop(row)">修改</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>


    <el-dialog title="修改门店账户" :visible.sync="isShowDialog" width="30%" destroy-on-close>
      <div>
        <el-form ref="shopUser" :model="shopUserForm" :rules="formRule" label-width="70px">
          <el-form-item label="账号" prop="userName">
            <el-input v-model.trim="shopUserForm.userName" placeholder="请输入门店账号(手机号)" disabled />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model.trim="shopUserForm.password" placeholder="请输入..." type="password" show-password />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button class="celBtn" @click="cancel">取消</el-button>
        <el-button class="addBtn" @click="submitShopUser">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getShopList, updateShopUser } from "@/api/shop";
import { regionData, CodeToText } from "element-china-area-data";
export default {
  name: "GoodsInfo",
  data() {
    return {
      linkUrl: "",
      options: regionData,
      selectedOptions: [],
      isSearch: false,
      selectedOptionsForm: {
        province: "",
        county: "",
        area: "",
      },
      addtions: {
        names: "",
      },
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      name: "",
      region: "",
      shopList: [],


      isShowDialog: false,
      shopUserForm: {
        id: "",
        userId: "",
        password: "",
      },
      formRule: {
        userName: [
          {
            required: true,
            message: "请输入门店账号",
            trigger: "blur",
          },
          {
            pattern:
              /^1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            pattern: /^([a-zA-Z0-9])+$/,
            message: "支持数字、大写英文字母",
            trigger: "blur",
          },
          {
            min: 0,
            max: 15,
            message: "密码不能超过15个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getShopList();
  },
  methods: {
    async getShopList() {
      if (!this.isSearch) {
        this.name = "";
        this.region = "";
      }
      const res = await getShopList({
        ...this.page,
        name: this.name,
        region: this.region,
      });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.shopList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log("shopList", this.shopList);
        const $table = this.$refs.xTable1
        $table.loadData(this.shopList);
      } else {
        // this.$message.error(res.message);
      }
    },
    addFn() {
      this.$router.push("shopAdd");
    },
    // 获取省市区
    handleChange(value) {
      this.selectedOptions = value;
      console.log(this.selectedOptions);
      var name = "";
      this.selectedOptions.map((item) => {
        name += CodeToText[item] + "/";
      }); // 将省市区三个拼接一起
      this.addtions.names = name;
      console.log(this.addtions.names); // 运用了CodeToText把编码转成了中文
      this.selectedOptionsForm.province = CodeToText[this.selectedOptions[0]];
      this.selectedOptionsForm.county = CodeToText[this.selectedOptions[1]];
      this.selectedOptionsForm.area = CodeToText[this.selectedOptions[2]];
      this.region = this.addtions.names;
      console.log(this.selectedOptionsForm);
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getShopList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getShopList();
    },
    shopDetail(row) {
      console.log(row);
      this.$router.push({
        path: "shopDetail",
        query: {
          type: "detail",
          id: row.id,
        },
      });
    },
    editShop(row) {
      this.$router.push({
        path: "shopEdit",
        query: {
          type: "edit",
          id: row.id,
        },
      });
    },
    editPassword(row) {
      this.shopUserForm.userName = row.userName ? row.userName : "";
      this.shopUserForm.password = "";
      this.shopUserForm.id = row.id ? row.id : "";
      this.shopUserForm.userId = row.userId ? row.userId : "";
      this.isShowDialog = true;
    },
    submitShopUser() {
      this.$refs.shopUser.validate(async (isOk) => {
        if (isOk) {
          console.log(this.shopUserForm);
          const res = await updateShopUser(this.shopUserForm);
          console.log(res.code);
          if (res.code === 200) {
            this.$message.success("修改成功");
            this.getShopList();
            this.isShowDialog = false;
          } else {
            // this.$message.error(res.message);
          }
        } else {
          this.$message.warning("请检查填写的信息");
        }
      });
    },
    cancel() {
      this.isShowDialog = false;
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.getShopList();
    },
    resetFn() {
      this.name = "";
      this.region = "";
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.selectedOptions = [];
      this.getShopList();
    },
    getLink(row) {
      const baseLink = "https://www.yukukeji.com/yuku/store?code=";
      console.log(baseLink + row.number);
      this.linkUrl = baseLink + row.number;
      const inputNode = document.createElement("input"); // 创建input
      inputNode.value = this.linkUrl; // 赋值给 input 值
      document.body.appendChild(inputNode); // 插入进去
      inputNode.select(); // 选择对象
      document.execCommand("Copy"); // 原生调用执行浏览器复制命令
      inputNode.className = "oInput";
      inputNode.style.display = "none"; // 隐藏
      this.$message.success("复制成功");
    },
  },
};
</script>

<style lang="scss" scoped></style>
